//
// Created by shenbin on 2022/4/17.
//

#ifndef ALGORITHM_TRAINNING_C02_LC227_H
#define ALGORITHM_TRAINNING_C02_LC227_H

#include<string>
#include<stack>
#include <vector>
#include "c02-lc150.h"
#include "c02-lc150.h"

using namespace std;

class LeetCode227 {
public:
    int calculate(string s) {
        vector<string> tokens;
        string val = "";
        s += " ";
        for (char ch: s) {
            if (ch >= '0' && ch <= '9') {
                val += ch;
                continue;
            } else {
                if (!val.empty()) {
                    tokens.push_back(val);
                    val = "";
                }
                if (ch == ' ') continue;
                int currRank = getOperatorRank(ch);
                /// ??
                while (!ops.empty() && getOperatorRank(ops.top()) >= currRank) {
                    tokens.push_back(string(1, ops.top()));
                    ops.pop();
                }
                ops.push(ch);
            }
        }
        while (!ops.empty()) {
            tokens.push_back(string(1, ops.top()));
            ops.pop();
        }
        LeetCode150 lc150;
        return lc150.evalRPN(tokens);

    }

private:
    stack<char> ops;

    int getOperatorRank(char ch) {
        if (ch == '*' || ch == '/') return 2;
        if (ch == '+' || ch == '-') return 1;
        return 0;
    }
};

#endif //ALGORITHM_TRAINNING_C02_LC227_H
